<?php
/*
	[Bidcms.com!] (C)2009-2011 Bidcms.com.
	This is NOT a freeware, use is subject to license terms
	$author limengqi
	$Id: createthumb.php 2010-08-24 10:42 $
*/
error_reporting(0);
header('Content-Type: image/jpeg');
$sitelist=array('2d6225490c'=>'http://localhost/pic/data/upload');
$sitekey=array('2d6225490c');
if(!empty($_GET['bidcmskey']))
{
	$len=strlen($_GET['bidcmskey']);
	if($len>=47)
	{
		$filedir="data/thumb";
		$rkey=$_GET['bidcmskey'];
		$sign=substr($rkey,0,10);
		if(!in_array($sign,$sitekey))
		{
			echo '';
			exit;
		}
		$filedir="data/thumb".'/'.$sign;
		if(!is_dir($filedir))
		{
			mkdir($filedir);
		}
		$filetype=substr($rkey,10,1);
		$filekey=substr($rkey,11,32);
		$rdir=substr($rkey,43,2).'/'.substr($rkey,45,2);
		$fw=explode('_',$rkey);
		$localfile=$filedir.'/'.$filekey;
		if(!empty($fw[1]))
		{
			$localfile.='_'.$fw[1];
		}
		if(!empty($fw[2]))
		{
			$localfile.='_'.$fw[2];
		}
		if(is_file($localfile))
		{
			echo file_get_contents($localfile);
			exit;
		}
		elseif(is_dir($filedir))
		{
			$remotehost=$sitelist[$sign];
			if(!empty($remotehost))
			{
				
					$remotefile=$remotehost.'/'.$rdir.'/'.$filekey;
					$imgtype=array('jpg','gif','png','bmp');
					if($filetype==1)
					{
						file_put_contents($localfile,file_get_contents($remotefile.'.gif'));
						echo file_get_contents($localfile);
						exit;
					}
					else
					{
						switch($imgtype[$filetype])
						{
							case 'gif':
								$im=@imagecreatefromgif($remotefile.'.gif');
								$remote=@getimagesize($remotefile.'.gif');
								$func='imagegif';
								break;
							case 'png':
								$im=@imagecreatefrompng($remotefile.'.png');
								$remote=@getimagesize($remotefile.'.png');
								$func='imagepng';
								break;
							case 'bmp':
								$im=@imagecreatefromwbmp($remotefile.'.bmp');
								$remote=@getimagesize($remotefile.'.bmp');
								$func='imagewbmp';
								break;
							default:	
								$im=@imagecreatefromjpeg($remotefile.'.jpg');
								$remote=@getimagesize($remotefile.'.jpg');
								$func='imagejpeg';
								break;
						}
						isset($fw[1]) && $width=preg_replace("#[^0-9]+#","",$fw[1]);
						isset($fw[2]) && $height=preg_replace("#[^0-9]+#","",$fw[2]);
						if($remote[0]>0)
						{
							if(!empty($width) && $width>0 && $remote[0]>=$width)
							{
								$w=$width;
								$src_width=$remote[0];
								$src_height=$remote[1];
								$src_x=0;
								$src_y=0;
								$h=ceil(($width/$remote[0])*$remote[1]);
								
								if($height>0)
								{
									if($h<$height)
									{
										$src_width=$src_width*($h/$height);
										$src_x=ceil(($remote[0]-$src_width)/2);
										
									}
									elseif($h>$height)
									{
										$src_height=$src_height*($height/$h);
										$src_y=ceil(($remote[1]-$src_height)/2);
									}
									$h=$height;
								}
								$dst=imagecreatetruecolor($w,$h);
								imagecopyresized($dst,$im,0,0,$src_x,$src_y,$w,$h,$src_width,$src_height);
								$func($dst,$localfile);
								$func($dst,null);
								imagedestroy($im);
								imagedestroy($dst);
							}
							else
							{
								$func($im,$localfile);
								$func($im);
								imagedestroy($im);
							}
							
						}
						else
						{
							if(is_file($filedir.'/normal_'.$width.'.jpg'))
							{
								echo file_get_contents($filedir.'/normal_'.$width.'.jpg');
							}
							elseif(is_file($filedir.'/default.jpg'))
							{
								echo file_get_contents($filedir.'/default.jpg');
							}
							else
							{
								echo file_get_contents('data/default.jpg');
							}
							exit;
						}
					}
			}
		}
		else
		{
			echo file_get_contents('data/default.jpg');
			exit;
		}
	}
	elseif($len>=10)
	{
		$sign=substr($_GET['bidcmskey'],0,10);
		if(in_array($sign,$sitekey))
		{
			echo file_get_contents('data/thumb/'.$sign.'/default.jpg');
			exit;
		}
		else
		{
			echo file_get_contents('data/default.jpg');
			exit;
		}
	}
	else
	{
		echo file_get_contents('data/default.jpg');
		exit;
	}
}